<?php
if ($_SERVER['SERVER_NAME'] == 'localhost') {
    $db_username = "root";
    $db_password = "Av5FmC4N6chnByW7";
    $db_server = "localhost";
    $db_name = "gs";
    
    $path = '/';
}
elseif($_SERVER['SERVER_NAME'] == 'dev.gstestequipment.com') {
    $db_username = "gstest";
    $db_password = "Av5FmC4N6chnByW7";
    $db_server = "mysql.gstestequipment.com";
    $db_name = "gstestequipment";
    
    $path = '/home/mikepaer_v1/dev.gstestequipment.com/';
}

// Connect
$db = new PDO('mysql:dbname=' . $db_name . ';host=' . $db_server . '', $db_username, $db_password);


// Classes
require_once($path . 'includes_php/classes/gsFeaturedItems.class.php');
require_once($path . 'includes_php/classes/gsLkBrand.class.php');
require_once($path . 'includes_php/classes/gsLkType.class.php');
require_once($path . 'includes_php/classes/gsProducts.class.php');
require_once($path . 'includes_php/classes/DFC.class.php');
require_once($path . 'includes_php/classes/DSC.class.php');

/*
 * -----------------------------------------------
 * Dumb Helper Functions
 * -----------------------------------------------
 */
function priceFormat($int) {
    return "$" . number_format($int, 0);
}

/*
 * -----------------------------------------------
 * Lookup Functions
 * -----------------------------------------------
 */

/**
 * Input: type name
 * Output: type ID
 *
 * @param type $strType
 * @return int id the id belonging to that type, otherwise null
 */
function lookup_idByType($strType) {
    global $db;

    $typeObj = new gsLkType();
    $typeObj->setName($strType);
    foreach (gsLkType::findByExample($db, $typeObj) as $member) {
	return $member->getId();
    }
    return null; // error
}

/**
 *
 * Gets the name of a type by ID
 * 
 * @global PDO $db
 * @param type $id
 * @return type 
 */
function lookup_typeById($id) {
    global $db;

    $typeObj = new gsLkType();
    $typeObj->setId($id);
    foreach (gsLkType::findByExample($db, $typeObj) as $member) {
	return $member->getName();
    }
    return null; // error
}


/**
 *
 * Gets the ID of a brand by name
 * 
 * @global PDO $db
 * @param type $strBrand
 * @return type 
 */
function lookup_idByBrand($strBrand) {
    global $db;

    $brandObj = new gsLkBrand();
    $brandObj->setName($strBrand);
    foreach (gsLkBrand::findByExample($db, $brandObj) as $member) {
	return $member->getId();
    }
    return null; // error
}

/**
 *
 * Gets the name of a brand by ID
 * 
 * @global PDO $db
 * @param int $id
 * @return string $type 
 */
function lookup_brandById($id) {
    global $db;

    $brandObj = new gsLkBrand();
    $brandObj->setId($id);
    foreach (gsLkBrand::findByExample($db, $brandObj) as $member) {
	return $member->getName();
    }
    return null; // error
}

/*
 * -----------------------------------------------
 * Non-Lookup Functions
 * -----------------------------------------------
 */

/**
 * returns an array of gsProducts for a particular category
 *
 * @global $db
 * @param string $type The type (category) to filter (id or string)
 * @param string $brand The brand (manufacturer) to filter (id or string)
 * @param DSC_Array $sortBy define dsc array for sortby (see: http://code.google.com/p/db2php/wiki/Examples)
 * @return gsProducts[]
 */
function getProducts($type=null, $brand=null, $sortBy=null) {
    global $db;

    $filteringExample = new gsProducts();
    
    if ($type != null) {
	if(is_numeric($type)) {
	    $typeId = $type;
	}
	else {
	    $typeId = lookup_idByType($type);
	    if ($typeId == null) {
		printf("Error: unable to find ID for type");
		exit(0);
	    }
	}
	
	$filteringExample->setLkType($typeId);
    }
	    
    if($brand != null) {
	if(is_numeric($brand)) {
	    $brandId = $brand;
	}
	else {
	    $brandId = lookup_idbyBrand($brand);
	    if ($brandId == null) {
		printf("Error: unable to find ID for brand");
		exit(0);
	    }
	}
	
	$filteringExample->setLkBrand($brandId);
    }
	
    return gsProducts::findByExample($db, $filteringExample, true, $sortBy);
}


/**
 * Returns an array of brand IDs for a given type
 * 
 * @param type $type int (id) or string (name) acceptable
 * @return array $collectedLkBrands an array containing the brand IDs that are available for a type
 */
function getBrandsByType($type) {
    $products = getProducts($type);
    $collectedLkBrands = array();

    foreach($products as $prod) {
	if(in_array($prod->getLkBrand(), $collectedLkBrands)) {
	    continue;
	}
	else {
	    $collectedLkBrands[] = $prod->getLkBrand();
	}
    }
    
    return $collectedLkBrands;
}

/**
 * Returns an array of type IDs for a given brand
 * 
 * @param brand $brand int (id) or string (name) acceptable
 * @return array $collectedLkTypes an array containing the type IDs that are available for a brand
 */
function getTypesByBrand($brand) {
    $products = getProducts(null, $brand);
    $collectedLkTypes = array();

    foreach($products as $prod) {
	if(in_array($prod->getLkType(), $collectedLkTypes)) {
	    continue;
	}
	else {
	    $collectedLkTypes[] = $prod->getLkType();
	}
    }
    
    return $collectedLkTypes;
}

/**
 * Counts the number of products listed for a certain brand, type, or neither
 * 
 * @param type $brand (id or name acceptable)
 * @param type $type (id or name acceptable)
 * @return int the count 
 */
function old_countProducts($type=null, $brand=null) {
    $products[] = new gsProducts();
    $products = getProducts($type, $brand);
    return count($products);
}

/**
 * Slightly faster version of old_countProducts
 * Counts the number of products listed for a certain brand, type, or neither
 * 
 * @param type $brand (id or name acceptable)
 * @param type $type (id or name acceptable)
 * @return int the count 
 */
function countProducts($type=null, $brand=null) {
    global $db_server; global $db_username; global $db_password; global $db_name;
    $sql = "SELECT id FROM products";
    if($type != null) {
	$sql .= " WHERE lk_type='". $type . "'";
	if($brand != null) {
	    $sql .= ' AND ';
	}
    }
    if($brand != null) {
	if($type == null) {
	    $sql .= 'WHERE ';
	}
	$sql .= "lk_brand='".$brand."'";
    }
    $db_local = mysql_connect($db_server, $db_username, $db_password) or die("Can't connect.");
    @mysql_select_db($db_name);
    $result = mysql_query($sql);
    mysql_close();
    return mysql_num_rows($result);
}

/**
 * Returns an array of IDs of ALL ENABLED types
 * or all types even if they are disabled (if specified.)
 * 
 * @global $db $db
 * @param bit enabledOnly A bit, so 1 or 0.
 * @return array $ret An array of IDs of types.
 */
function getAllTypes($enabledOnly=true, $featuredOnly=false) {
    global $db;
    $types = array();

    $example = new gsLkType();
    
    if($enabledOnly) {
	$example->setEnabled(1);
    }
    
    if($featuredOnly) {
	$example->setFeaturedNav(1);
    }
    
    $types = gsLkType::findByExample($db, $example);
    
    $ret = array();
    
    foreach($types as $ty) {
	$ret[] = $ty->getId();
    }
    
    return $ret;
}

/**
 * Returns an array of IDs of ALL ENABLED brands
 * or all brands even if disabled (if specified.)
 * 
 * @global $db $db
 * @param bit enabledOnly A bit, so 1 or 0.
 * @return array $ret An array of IDs of brands.
 */
function getAllBrands($enabledOnly=true, $featuredOnly=false) {
    global $db;
    $brands = array();
    
    $example = new gsLkBrand();
    
    if($enabledOnly) {
	$example->setEnabled(1);
    }
    
    if($featuredOnly) {
	$example->setFeaturedNav(1);
    }
    
    $brands = gsLkBrand::findByExample($db, $example);
    
    $ret = array();
    
    foreach($brands as $br) {
	$ret[] = $br->getId();
    }
    
    return $ret;
}

/**
 * Echos the pagination code with ajax.
 * 
 * @param type $currentlyOn 3,4,5
 * @param type $totalItems 655
 * @param type $itemsPerPage 50
 * @param type $typeId 47
 * @param type $brandId 25
 * @param type $sortBy products__desc
 * @return type 
 */
function paginate($currentlyOn, $totalItems, $itemsPerPage, $typeId, $brandId, $sortBy=null) {

    echo "<ul>";
    for ($i = 0; $i < ceil($totalItems / $itemsPerPage); $i++) {

	$url = "browse.php?type=" . $typeId . "&&brand=" . $brandId . "&&page=" . $i;
	$javascript = "javascript:paginate(" . $typeId . ", " . $brandId . ", " . $i;
	
	if (!empty($sortBy)) {
	    $url = $url . "&&sortBy=" . $sortBy;
	    $javascript = $javascript . ", '" . $sortBy . "'";
	}
	    
	$javascript = $javascript . "); return false;";

	echo "<li";
	if($currentlyOn == $i) {
	    echo " id=\"active\"";
	}
	echo ">";
	if($currentlyOn == $i) {
	    echo "<a class=\"paginationButton\">";
	}
	else {
	    echo "<a href=\"" . $url . "\" onclick=\"". $javascript ."\" class=\"paginationButton\">";
	}
	echo ($i + 1);
	echo "</a></li>";
    }
    echo "</ul>";
    return;
}

/*
 * -----------------------------------------------
 * Tests
 * -----------------------------------------------
 */

// Test 1: test to see if I can retrieve products by category
//$products[] = new gsProducts();
//$products = getProducts("Probes", null);
//
//for ($i = 0; $i < sizeof($products); $i++) {
//    echo $products[$i]->getId() . " -- [" . lookup_brandbyId($products[$i]->getLkBrand()) . "] " .$products[$i]->getModelNo() . "<br>";
//    //if($i == 1000) break;
//}
// End test
?>
